//
//  JHMainViewController.swift
//  JHWeiBo
//
//  Created by qujiahong on 2019/3/21.
//  Copyright © 2019 iOS_July. All rights reserved.
//

import UIKit

class JHMainViewController: UITabBarController {

    // MARK: - 中间的 + 号 按钮
    private lazy var composeButton : UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupChildControllers()
        setupComposeButton()
    }

    // MARK: - 中间按钮 监听事件
    // FIXME: 没有实现的
    @objc private func clickComposeBtn() {
        // private 添加为私有
        // @objc 允许这个函数在运行时通过OC的消息机制被调用
        print("点击中间按钮")
    }
    

}

// extension 分类，不可以定义属性，可以用来切分代码块
// 把功能相近的函数，放在一个 extension k里
// 便于代码维护
// MARK: - 设置界面
extension JHMainViewController {
    
    // MARK: - 中间 + 号按钮
    private func setupComposeButton() {
        tabBar.addSubview(composeButton)
        let count = CGFloat(children.count)
        // 将向内缩的宽度减少，能够让按钮的宽度变大，盖住容错点，防止穿帮
        let w = tabBar.bounds.width/count - 1
        // CGRectInset 证书向内收缩、负数向外扩展
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        composeButton.addTarget(self, action: #selector(clickComposeBtn), for: .touchUpInside)
    }
    
    /// 设置所有 子控制器
    private func setupChildControllers() {
        
        let array = [
            ["clsName" : "JHHomeViewController", "title" : "首页", "imageName": "tabbar_home"],
            ["clsName" : "JHMessageViewController", "title" : "消息", "imageName": "tabbar_message_center"],
            ["clsName" : "UIViewController"],
            ["clsName" : "JHDiscoverViewController", "title" : "发现", "imageName": "tabbar_discover"],
            ["clsName" : "JHProfileViewController", "title" : "我", "imageName": "tabbar_profile"]
        ]
        
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(setupController(dict: dict ))
        }
        viewControllers = arrayM
    }
    
    
    /// 使用字典创建一个子控制器
    ///
    /// - Parameter dict: 信息字典[clsName, title, imageName]
    /// - Returns: 一个子控制器
    private func setupController(dict: [String : String]) -> UIViewController {
        
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
        else {
            return UIViewController()
        }
        //print("\(clsName)\(title)\(imageName)")
        
        let vc = cls.init()
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: (imageName+"_selected"))?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.orange], for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 13)], for: .normal)//系统默认是12，且需 设置normal状态才行
        
        //实例化导航控制器，会调用push方法，将rootVC压栈
        let nav = JHNavigationController.init(rootViewController: vc)
        
        
        return nav
    }
}

/*
let array = [
    ["clsName" : "HomeViewController", "title" : "首页", "imageName": "tabbar_home",  "visitorInfo" :
        ["imageName": "", "message" : "关注一些人，回这里看看有什么惊喜" ]],
    ["clsName" : "MessageViewController", "title" : "消息", "imageName": "message_center",  "visitorInfo" :
        ["imageName": "visitordiscover_image_message", "message" : "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知" ]],
    ["clsName" : "UIViewController"],
    ["clsName" : "DiscoverViewController", "title" : "发现", "imageName": "discover",  "visitorInfo" :
        ["imageName": "visitordiscover_image_message", "message" : "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过" ]],
    ["clsName" : "ProfileViewController", "title" : "我", "imageName": "profile",  "visitorInfo" :
        ["imageName": "visitordiscover_image_profile", "message" : "登录后，你的微博、相册、个人资料会显示在这里，展示给别人" ]]
]
*/
